package com.ruoyi.order.controller;

import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.service.TokenService;


import com.ruoyi.order.api.RemoteActiveShop;
import com.ruoyi.order.api.RemoteCfMyService;
import com.ruoyi.order.api.RemoteCfServiceService;
import com.ruoyi.order.api.RemoteCfServiceSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.order.domain.CfOrder;
import com.ruoyi.order.service.ICfOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单管理Controller
 * 
 * @author ruoyi
 * @date 2024-06-04
 */
@RestController
@RequestMapping("/order")
public class CfOrderController extends BaseController
{
    @Autowired
    private ICfOrderService cfOrderService;

    @Autowired
    private RemoteCfServiceService remoteCfServiceService;

    @Autowired
    private RemoteCfMyService myService;

    @Autowired
    private RemoteCfServiceSort remoteCfServiceSort;

    @Autowired
    private RemoteActiveShop remoteActiveShop;










//    @GetMapping("/findById")
//    public AjaxResult test(Long id){
//        AjaxResult info = myService.getInfo(id);
//
//        // 解析JSON字符串
//        JSONObject jsonObject = new JSONObject(info);
//
//        // 获取data字段
//        JSONObject dataObject = jsonObject.getJSONObject("data");
//        String username = dataObject.getString("username");
//        System.out.println(username+"================");

//        AjaxResult sortInfo = remoteCfServiceSort.getInfo(id);
//
//        return sortInfo;
//    }

    /**
     * 查询订单管理列表
     */
    @RequiresPermissions("order:order:list")
    @GetMapping("/list")
    public TableDataInfo list(CfOrder cfOrder)
    {
        startPage();
        List<CfOrder> list = cfOrderService.selectCfOrderList(cfOrder);
        Iterator<CfOrder> iterator = list.iterator();
        while (iterator.hasNext()){
            CfOrder order = iterator.next();
//            System.out.println(order);

                AjaxResult myServiceInfo = myService.getInfo(order.getUserId());
                JSONObject jsonObject = new JSONObject(myServiceInfo);
                JSONObject data = jsonObject.getJSONObject("data");

                order.setUsername(data.getString("username"));
                order.setPhone(data.getString("phone"));




//
//
                AjaxResult serviceInfo = remoteCfServiceSort.getInfo(order.getServiceId());
                JSONObject jsonObject1 = new JSONObject(serviceInfo);
                JSONObject data1 = jsonObject1.getJSONObject("data");

                    order.setServiceName(data1.getString("name"));




                AjaxResult shopInfo = remoteActiveShop.getInfo(order.getShopId());
                JSONObject jsonObject3 = new JSONObject(shopInfo);
                JSONObject data3 = jsonObject3.getJSONObject("data");
                order.setShopName(data3.getString("name"));

        }
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @RequiresPermissions("order:order:export")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CfOrder cfOrder)
    {
        List<CfOrder> list = cfOrderService.selectCfOrderList(cfOrder);
        ExcelUtil<CfOrder> util = new ExcelUtil<CfOrder>(CfOrder.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
//    @RequiresPermissions("order:order:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {

        return success(cfOrderService.selectCfOrderById(id));
    }

    /**
     * 新增订单管理
     */
//    @RequiresPermissions("order:order:add")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CfOrder cfOrder)
    {
        return toAjax(cfOrderService.insertCfOrder(cfOrder));
    }

    /**
     * 修改订单管理
     */
//    @RequiresPermissions("order:order:edit")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CfOrder cfOrder)
    {
        return toAjax(cfOrderService.updateCfOrder(cfOrder));
    }

    /**
     * 删除订单管理
     */
    @RequiresPermissions("order:order:remove")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cfOrderService.deleteCfOrderByIds(ids));
    }
}
